class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        while (head != NULL && head->val == val) { 
            ListNode* t = head;
            head = head->next;
            delete t;
        }
        ListNode* c=head;
        while (c!=NULL&&c->next!=NULL) {
            if (c->next->val==val) {
                ListNode* t=c->next;
                c->next=c->next->next;
                delete t;
            } else {
                c= c->next;
            }
        }
        return head;
    }
};